<?php

/**
 * @author trongkhoi
 */
class OptionController extends CrudController
{

    public $modelName = 'Option';
    public $title = 'Option';

    public function init()
    {
        parent::init();
        $this->indexColumns = array(
//            array(
//                'class' => 'CCheckBoxColumn',
//                'id' => 'ids',
//                'selectableRows' => 2,
//            ),
            array(
                'header' => Yii::t('user', 'Name'),
                'name' => 'name',
                'type' => 'raw',
                'value' => 'CHtml::link($data->name,$data->editUrl,array("target"=>"blank"))',
            ),
            array(
                'header' => Yii::t('user', 'Value'),
                'name' => 'value',
                'type' => 'raw',
                'value' => '$data->data_type=="'.Option::TYPE_IMAGE.'"?CHtml::image(baseUrl()."/".$data->value,"",array("width"=>120,"height"=>120)) :$data->value',
            ),
            array(
                'header' => Yii::t('user', 'Description'),
                'name' => 'description',
                'type' => 'raw',
                'value' => '$data->description',
            ),
            array('class' => 'application.widgets.MyButtonColumn',
                'editUrl' => '$data->editUrl',
                'deleteUrl' => '#',
            ),
        );
        $this->formColumns = array('name', 'value', 'description',
            'category' => array(
                'type' => 'radioButtonList',
                'data' => Option::getArrayCategory(),
            ),
            'data_type' => array(
                'type' => 'dropDownList',
                'data' => Option::getArrayType(),
            )
        );
    }

    function actionFile()
    {
        $this->checkAccess();
        $this->render('file');
    }

    public function actionConfig()
    {
        $this->checkAccess();
        if (isset($_FILES['Option']))
        {
            $optsPhoto = $this->_uploadOptionPhoto('option', $_FILES['Option']);
            foreach ($optsPhoto as $id => $value)
            {
                $option = Option::model()->findByPk($id);                
                if ($option != NULL)
                {
                    $option->value = $value;
                    $option->update(array('value'));
                }
            }
        }

        if (isset($_POST['Option']))
        {
            foreach ($_POST['Option'] as $id => $value)
            {
                $option = Option::model()->findByPk($id);
                if ($option != NULL)
                {
                    $option->value = $value;
                    $option->update(array('value'));
                }
            }            
        }
        
        $opts = Option::model()->findAll($this->criteria());
        $this->render('config', array('options' => $opts));
    }
    
    
    protected function criteria()
    {
        $crit = new CDbCriteria();
//        $crit->condition = 'category=:c';
//        $crit->params = array(':c' => Option::CAT_HEADER);
        
        return $crit;
    }

    protected function _uploadOptionPhoto($dir, $file)
    {
        $iArray = array();
        if (@$file)
        {
            foreach ($file['name'] as $fkey => $fname)
            {
                // Lấy extension của file và tạo tên file mới
                $ext = pathinfo($fname, PATHINFO_EXTENSION);
                $tmp_file = "files/$dir/" . md5(time() . $fname) . ".$ext";

                // Move file vào thư mục cần lưu                
                if (@move_uploaded_file($file['tmp_name'][$fkey], $tmp_file))
                {
                    $iArray[$fkey] = $tmp_file;
                }
            }
        }
        return $iArray;
    }
}
